from crm import models
from django import forms
from django.forms import widgets,fields
from crm import kingadmin

class form(forms.ModelForm):
    def __new__(cls, *args, **kwargs):

        for field in cls.base_fields:
            if field in cls.Meta.readonly_fields:
                field_obj=cls.base_fields[field]
                field_obj.widget.attrs.update({'disabled':'true'})
        return forms.ModelForm.__new__(cls)
    class Meta:
        fields='__all__'
        model=models.CustomerInfo
        readonly_fields = ['status','name','source']

    def clean(self):
        if self.errors:
            raise forms.ValidationError('请按正确要求填写表单')
        if self.instance.id is not None:
            for field in self.Meta.readonly_fields:
                old_data=getattr(self.instance,field)
                new_data=self.cleaned_data.get(field)
                if old_data != new_data:

                    self.add_error(field,'请不要试图改变只读字段！！')
        if self.has_changed():
            print('数据已改变')
        else:
            print('数据完整')

